package com.mewlxy.itemchooseview.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mewlxy.itemchooseview.R;


/**
 * Created by master on 2017/6/28.
 */

public class ChooseItemView extends LinearLayout
{
    private final int itemNameTextPaddingLeft;
    private final String editHint;
    private final int editHintColor;
    private final String editText;
    private final int editTextColor;
    private final int editTextSize;
    private Context context;
    private String itemNameText;
    private int itemNameTextColor;
    private int itemNameTextSize;
    private String itemChooseText;
    private int itemChooseTextColor;
    private int itemChooseTextSize;
    private int itemChooseGravity;
    private OnClickListener iconClickListener;
    private boolean iconVisible;
    private boolean editMode;
    private TextView tvItemChoose;
    private TextView tvItemName;
    private EditText etItemChoose;
    private int itemHeight;
    private LinearLayout llContainer;
    private Paint paint;
    private int separateColor;
    private int separateHeight;
    private int separateMarinLeft;
    private int separateMarinRight;
    private int editTextGravity;
    private TextWatcher textChangeListener;

    public ChooseItemView(Context context)
    {
        this(context, null, 0);
    }

    public ChooseItemView(Context context, AttributeSet attrs)
    {
        this(context, attrs, 0);
    }

    public ChooseItemView(Context context, AttributeSet attrs, int defStyleAttr)
    {
        super(context, attrs, defStyleAttr);
        this.context = context;
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ChooseItemView);
        itemNameText = typedArray.getString(R.styleable.ChooseItemView_item_name_text);
        itemNameTextPaddingLeft = (int) typedArray.getDimension(R.styleable.ChooseItemView_item_name_text_padding_left, dip2px(15));
        itemNameTextColor = typedArray.getColor(R.styleable.ChooseItemView_item_name_text_color, Color.BLACK);
        itemNameTextSize = (int) typedArray.getDimension(R.styleable.ChooseItemView_item_name_text_size, 16);
        itemChooseText = typedArray.getString(R.styleable.ChooseItemView_item_choose_text);
        itemChooseTextColor = typedArray.getColor(R.styleable.ChooseItemView_item_choose_text_color, Color.BLACK);
        itemChooseTextSize = (int) typedArray.getDimension(R.styleable.ChooseItemView_item_choose_text_size, 16);
        itemChooseGravity = typedArray.getInt(R.styleable.ChooseItemView_item_choose_gravity, 2);
        editTextGravity = typedArray.getInt(R.styleable.ChooseItemView_item_edit_text_gravity, 2);
        iconVisible = typedArray.getBoolean(R.styleable.ChooseItemView_item_choose_icon_visible, true);

        editMode = typedArray.getBoolean(R.styleable.ChooseItemView_item_edit_mode, false);
        editHint = typedArray.getString(R.styleable.ChooseItemView_item_edit_hint);
        editHintColor = typedArray.getColor(R.styleable.ChooseItemView_item_edit_hint_color, Color.LTGRAY);
        editText = typedArray.getString(R.styleable.ChooseItemView_item_edit_text);
        editTextColor = typedArray.getColor(R.styleable.ChooseItemView_item_edit_text_color, Color.BLACK);
        editTextSize = (int) typedArray.getDimension(R.styleable.ChooseItemView_item_edit_text_size, 16);

        separateColor = typedArray.getColor(R.styleable.ChooseItemView_item_separate_color, Color.LTGRAY);
        separateHeight = (int) typedArray.getDimension(R.styleable.ChooseItemView_item_separate_height, dip2px(2));
        separateMarinLeft = (int) typedArray.getDimension(R.styleable.ChooseItemView_item_separate_margin_left, dip2px(15));
        separateMarinRight = (int) typedArray.getDimension(R.styleable.ChooseItemView_item_separate_margin_right, 0);
        itemHeight = (int) typedArray.getDimension(R.styleable.ChooseItemView_item_height, dip2px(50));

        typedArray.recycle();
        initView();
    }


    private void initView()
    {
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(separateColor);
        paint.setStrokeWidth(separateHeight);
        View view = LayoutInflater.from(context).inflate(R.layout.layout_choose_item, this);

        llContainer = (LinearLayout) view.findViewById(R.id.ll_view_container);
        tvItemName = (TextView) view.findViewById(R.id.tv_choose_item_name);
        tvItemChoose = (TextView) view.findViewById(R.id.tv_choose_item_choose);
        etItemChoose = (EditText) view.findViewById(R.id.et_choose_item_choose);

        tvItemName.setText(itemNameText);
        tvItemName.setTextColor(itemNameTextColor);
        tvItemName.setTextSize(itemNameTextSize);
        tvItemName.setPadding(itemNameTextPaddingLeft, 0, 0, 0);

        tvItemChoose.setText(itemChooseText);
        tvItemChoose.setTextColor(itemChooseTextColor);
        tvItemChoose.setTextSize(itemChooseTextSize);

        ViewGroup.LayoutParams params = llContainer.getLayoutParams();
        params.height = itemHeight;
        llContainer.setLayoutParams(params);

        etItemChoose.setText(editText);
        etItemChoose.setHint(editHint);
        etItemChoose.setHintTextColor(editHintColor);
        etItemChoose.setTextSize(editTextSize);
        etItemChoose.setTextColor(editTextColor);
        if (editMode)
        {
            etItemChoose.setVisibility(VISIBLE);
            tvItemChoose.setVisibility(GONE);

            iconVisible = false;
        } else
        {
            etItemChoose.setVisibility(GONE);
            tvItemChoose.setVisibility(VISIBLE);
        }


        if (!iconVisible)
        {
            tvItemChoose.setCompoundDrawables(null, null, null, null);
        }

        switch (itemChooseGravity)
        {
            case 0:
                tvItemChoose.setGravity(Gravity.CENTER_VERTICAL | Gravity.START);
                break;
            case 1:
                tvItemChoose.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER);
                break;
            case 2:
                tvItemChoose.setGravity(Gravity.CENTER_VERTICAL | Gravity.END);
                break;
        }

        switch (editTextGravity)
        {
            case 0:
                etItemChoose.setGravity(Gravity.CENTER_VERTICAL | Gravity.START);
                break;
            case 1:
                etItemChoose.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER);
                break;
            case 2:
                etItemChoose.setGravity(Gravity.CENTER_VERTICAL | Gravity.END);
                break;
        }


        this.post(new Runnable()
        {

            @Override
            public void run()
            {
                if (iconClickListener != null)
                {
                    tvItemChoose.setOnClickListener(iconClickListener);
                }
                if (textChangeListener != null)
                {
                    etItemChoose.addTextChangedListener(textChangeListener);
                }
            }
        });
    }

    @Override
    protected void dispatchDraw(Canvas canvas)
    {
        super.dispatchDraw(canvas);
        canvas.drawLine(separateMarinLeft, itemHeight, getWidth() - separateMarinRight, itemHeight, paint);
    }


    private int dip2px(int dp)
    {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics());
    }

    public void setIconClickListener(OnClickListener iconClickListener)
    {
        this.iconClickListener = iconClickListener;
    }

    public String getItemNameText()
    {
        return tvItemName.getText().toString().trim();
    }

    public void setItemNameText(String itemNameText)
    {
        this.itemNameText = itemNameText;
        tvItemName.setText(itemNameText);
    }


    public void setItemNameTextColor(int itemNameTextColor)
    {
        this.itemNameTextColor = itemNameTextColor;
        tvItemName.setTextColor(ContextCompat.getColor(context, itemNameTextColor));
    }

    public int getItemNameTextSize()
    {
        return (int) tvItemName.getTextSize();
    }

    public void setItemNameTextSize(int itemNameTextSize)
    {
        this.itemNameTextSize = itemNameTextSize;
        tvItemName.setTextSize(TypedValue.COMPLEX_UNIT_SP, itemNameTextSize);
    }

    public String getItemChooseText()
    {
        if (tvItemChoose.getVisibility() == VISIBLE)
        {
            return tvItemChoose.getText().toString();
        } else
        {
            return etItemChoose.getText().toString();
        }
    }

    public void setItemChooseText(String itemChooseText)
    {
        this.itemChooseText = itemChooseText;
        tvItemChoose.setText(itemChooseText);
    }


    public void setItemChooseTextColor(int itemChooseTextColor)
    {
        this.itemChooseTextColor = itemChooseTextColor;
        tvItemChoose.setTextColor(ContextCompat.getColor(context, itemChooseTextColor));
    }

    public int getItemChooseTextSize()
    {
        return (int) tvItemChoose.getTextSize();
    }

    public void setItemChooseTextSize(int itemChooseTextSize)
    {
        this.itemChooseTextSize = itemChooseTextSize;
        tvItemChoose.setTextSize(TypedValue.COMPLEX_UNIT_SP, itemChooseTextSize);
    }

    public int getItemChooseGravity()
    {
        return tvItemChoose.getGravity();
    }

    public void setItemChooseGravity(int itemChooseGravity)
    {
        this.itemChooseGravity = itemChooseGravity;
        switch (itemChooseGravity)
        {
            case 0:
                tvItemChoose.setGravity(Gravity.CENTER_VERTICAL | Gravity.START);
                break;
            case 1:
                tvItemChoose.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER);
                break;
            case 2:
                tvItemChoose.setGravity(Gravity.CENTER_VERTICAL | Gravity.END);
                break;
        }
    }

    public int getEditTextGravity()
    {
        return etItemChoose.getGravity();
    }

    public void setEditTextGravity(int editTextGravity)
    {
        this.editTextGravity = editTextGravity;
        switch (editTextGravity)
        {
            case 0:
                etItemChoose.setGravity(Gravity.CENTER_VERTICAL | Gravity.START);
                break;
            case 1:
                etItemChoose.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER);
                break;
            case 2:
                etItemChoose.setGravity(Gravity.CENTER_VERTICAL | Gravity.END);
                break;
        }
    }

    public float getItemNameTextPaddingLeft()
    {
        return tvItemName.getPaddingLeft();
    }

    public boolean isIconVisible()
    {
        return tvItemChoose.getCompoundPaddingRight() == 0;
    }

    public void setIconVisible(boolean iconVisible)
    {
        this.iconVisible = iconVisible;
        if (iconVisible)
        {
            Drawable drawable = ContextCompat.getDrawable(context, R.drawable.icon_jiantou_right);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            tvItemChoose.setCompoundDrawables(null, null, drawable, null);
        } else
        {
            tvItemChoose.setCompoundDrawables(null, null, null, null);
        }
    }

    public boolean isEditMode()
    {
        return etItemChoose.getVisibility() == VISIBLE;
    }

    public void setEditMode(boolean editMode)
    {
        this.editMode = editMode;
        if (editMode)
        {
            etItemChoose.setVisibility(VISIBLE);
        } else
        {
            etItemChoose.setVisibility(GONE);
        }
    }

    public int getItemHeight()
    {
        return getHeight();
    }

    public void setItemHeight(int itemHeight)
    {
        this.itemHeight = itemHeight;
        ViewGroup.LayoutParams params = llContainer.getLayoutParams();
        params.height = dip2px(itemHeight);
        llContainer.setLayoutParams(params);

    }


    public String getEditHint()
    {
        return etItemChoose.getHint().toString();
    }

    public int getEditHintColor()
    {
        return editHintColor;
    }

    public String getEditText()
    {
        return etItemChoose.getText().toString().trim();
    }

    public int getEditTextColor()
    {
        return editTextColor;
    }

    public int getEditTextSize()
    {
        return (int) etItemChoose.getTextSize();
    }

    public int getSeparateColor()
    {
        return separateColor;
    }

    public void setSeparateColor(int separateColor)
    {
        this.separateColor = separateColor;
        paint.setColor(ContextCompat.getColor(context,separateColor));
        invalidate();
    }

    public int getSeparateHeight()
    {
        return (int) paint.getStrokeWidth();
    }

    public void setSeparateHeight(int separateHeight)
    {
        this.separateHeight = separateHeight;
        paint.setStrokeWidth(dip2px(separateHeight));
        invalidate();

    }

    public int getSeparateMarinLeft()
    {
        return separateMarinLeft;
    }

    public void setSeparateMarinLeft(int separateMarinLeft)
    {
        this.separateMarinLeft = separateMarinLeft;
        invalidate();
    }

    public int getSeparateMarinRight()
    {
        return separateMarinRight;
    }

    public void setSeparateMarinRight(int separateMarinRight)
    {
        this.separateMarinRight = separateMarinRight;
        invalidate();
    }

    public TextWatcher getTextChangeListener()
    {
        return textChangeListener;
    }

    public void setTextChangeListener(TextWatcher textChangeListener)
    {
        this.textChangeListener = textChangeListener;
    }
}
